/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBean;

import HouseBN.HouseService;
import LinkImageBN.LinkimageService;
import SaveMessage.SavemessageService;
import UserBN.Authentication;
import UserBN.UserService;
import entity.House;
import entity.LinkImage;
import entity.Savemessage;
import entity.SwhUser;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Mr-Quick
 */
@ManagedBean
@ViewScoped
public class MessageMB {

    public MessageMB() {
        getallmymessage();
    }
    String idhouse;
    List<House> lh;
    House house;
    SwhUser user;

    public String getIdhouse() {
        return idhouse;
    }

    public void setIdhouse(String idhouse) {
        this.idhouse = idhouse;
    }

    public House getHouse() {
        return house;
    }

    public void setHouse(House house) {
        this.house = house;
    }

    public List<House> getLh() {
        return lh;
    }

    public void getallmymessage() {
        Authentication a = Authentication.getInstance();
        UserService us = new UserService();
        String username = a.getUsername();
        user = us.findAccountByUsername(username);
        HouseService hs = new HouseService();
        lh = hs.Getallhousebyiduser(user);
    }

    public String deleteonehouse() {
        try {
            HouseService hs = new HouseService();
            house = hs.Gethousebyid(idhouse);
            LinkimageService lis = new LinkimageService();
            List<LinkImage> listimg = lis.Getlinkimagebyidhouse(idhouse);
            SavemessageService sms = new SavemessageService();
            List<Savemessage> ls = sms.Findallmessagebyidhouse(house);
            if (listimg != null) {
                lis.deleteimagebyiduser(listimg);
                if(ls != null)
                {
                    sms.deletesavemessagebyidhouse(ls);
                }
            }
            hs.deleteonehousebyidhouse(house);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Delete Success", "Add in Your Message Success");
            FacesContext.getCurrentInstance().addMessage(null, message);
            return "/Mymessages.xhtml?faces-redirect=true";
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "System Error", "This Message had in your message");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        return null;
    }
}
